package com.example.tellwin.mine.act;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.example.tellwin.AppHelper;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.event.MineEvent;
import com.example.tellwin.mine.bean.UserInfo;
import com.example.tellwin.mine.contract.CreditsExchangeContract;
import com.example.tellwin.mine.presenter.CreditsExchangePresenter;
import com.example.tellwin.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreditsExchangeActivity extends CpBaseActivty implements CreditsExchangeContract.View {

    @BindView(R.id.credits_tv)
    TextView creditsTv;
    @BindView(R.id.credits_et)
    EditText creditsEt;
    @BindView(R.id.credits_text)
    TextView creditsText;
    @BindView(R.id.exchange_btn)
    TextView exchangeBtn;
    @BindView(R.id.credits_exchange_tv)
    TextView creditsExchangeTv;

    UserInfo userInfo;

    @Inject
    CreditsExchangePresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits_exchange);
        ButterKnife.bind(this);
        userInfo = AppHelper.getInstance().getUserInfo();
        if (userInfo == null) {
            ToastUtil.show(this, R.string.get_user_info_error);
            finish();
        }
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void initDatas() {
        setTitle(R.string.credits);
        creditsTv.setText(userInfo.getIntegral() + "");
        mPresenter.getRegulation();
    }

    @OnClick(R.id.exchange_btn)
    public void onViewClicked() {
        mPresenter.creditsExchange(creditsEt.getText().toString());
    }

    @Override
    public void regulationSuccess(int integralSet) {
        creditsExchangeTv.setText(getResources().getString(R.string.credits_exchange_hint , integralSet+""));
    }

    @Override
    public void creditsExchangeSuccess() {
        finish();
        EventBus.getDefault().post(new MineEvent());
    }
}
